﻿Imports System.Xml
Imports System.Data
Imports System.Data.SqlClient
Imports Excel = Microsoft.Office.Interop.Excel




Public Class MainForm

   Public msLogFile As String = ""
   Public mdsLogFile As New DataSet

   Private Sub LoadXmlInDataSet()
      If msLogFile.Length = 0 Then
         Exit Sub
      End If

      ' Check: Well Formed XML
      Try
         mdsLogFile.ReadXml(msLogFile)
      Catch ex As Exception
         MsgBox("Open failed. The XML file is not well formed." & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Critical)
         Exit Sub
      End Try

      ' Check: Valid XML
      If mdsLogFile.Tables.Count <> 1 Then
         MsgBox("Open failed. The XML file is not structured as needed.", MsgBoxStyle.Critical)
         Exit Sub
      End If

      mdsLogFile.AcceptChanges()
      dgvLogFile.DataSource = mdsLogFile
      dgvLogFile.DataMember = mdsLogFile.Tables(0).TableName
   End Sub

   Private Sub tsmiOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiOpen.Click
      ofdOpen.Title = "Open a XML Log File"
      ofdOpen.Filter = "XML File|*.xml"
      If ofdOpen.ShowDialog() = DialogResult.OK Then
         msLogFile = ofdOpen.FileName
         LoadXmlInDataSet()
      End If
   End Sub

   Private Sub tsmiClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiClose.Click
        If msLogFile.Length > 0 Then
            msLogFile = ""
            mdsLogFile.Dispose()
            dgvLogFile.DataSource = Nothing
        End If
   End Sub

   Private Sub tsmiQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiQuit.Click
      Me.Close()
   End Sub

   Private Sub tsmiCharts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiCharts.Click
        MdiParentChart.ShowDialog()
    End Sub

   Private Sub tsmiConverter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiConverter.Click
      ConverterDialog.ShowDialog()
   End Sub

   Private Sub tsmiManual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiManual.Click
      System.Diagnostics.Process.Start("http://code.google.com/p/it4701teithegr-project126/wiki/Manual")
   End Sub

   Private Sub tsmiAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiAbout.Click
      AboutDialog.ShowDialog()
   End Sub

   Private Sub MainForm_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
      dgvLogFile.Size = Me.Size - New Size(40, 77)
   End Sub

   Private Sub tsmiEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsmiEdit.Click
      If tsmiEdit.Checked Then
         tsEditToolbar.Visible = True
         dgvLogFile.ReadOnly = False
         dgvLogFile.Size = dgvLogFile.Size - New Size(0, 25)
         dgvLogFile.Location = New Point(dgvLogFile.Location.X, dgvLogFile.Location.Y + 25)
      Else
         tsEditToolbar.Visible = False
         dgvLogFile.ReadOnly = True
         dgvLogFile.Size = dgvLogFile.Size + New Size(0, 25)
         dgvLogFile.Location = New Point(dgvLogFile.Location.X, dgvLogFile.Location.Y - 25)
      End If
   End Sub

   Private Sub tsbSaveToXml_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbSaveToXml.Click
      If msLogFile.Length > 0 Then
         mdsLogFile.WriteXml(msLogFile)
      End If
   End Sub

   Private Sub tsbReloadXml_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbReloadXml.Click
      If msLogFile.Length > 0 Then
         mdsLogFile.Clear()
         dgvLogFile.DataSource = Nothing
         LoadXmlInDataSet()
      End If
   End Sub

   Private Sub tsbSaveToSession_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbSaveToSession.Click
      If msLogFile.Length > 0 Then
         mdsLogFile.AcceptChanges()
      End If
   End Sub

   Private Sub tsbUndo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbUndo.Click
      If msLogFile.Length > 0 Then
         mdsLogFile.RejectChanges()
      End If
   End Sub

   Private Sub tsbCut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbCut.Click
      If dgvLogFile.GetCellCount(DataGridViewElementStates.Selected) > 0 Then
         Try
            Clipboard.SetDataObject(dgvLogFile.GetClipboardContent)
            For Each selectedCell As DataGridViewCell In dgvLogFile.SelectedCells
               selectedCell.Value = Nothing
            Next selectedCell
         Catch ex As Exception
            MsgBox("Could not Cut the selected cells.", MsgBoxStyle.Exclamation)
         End Try
      End If
   End Sub

   Private Sub tsbCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbCopy.Click
      If dgvLogFile.GetCellCount(DataGridViewElementStates.Selected) > 0 Then
         Try
            Clipboard.SetDataObject(dgvLogFile.GetClipboardContent)
         Catch ex As Exception
            MsgBox("Could not Copy the selected cells.", MsgBoxStyle.Exclamation)
         End Try
      End If
   End Sub

   Private Sub tsbDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbDelete.Click
      For Each selectedRow As DataGridViewRow In dgvLogFile.SelectedRows
         dgvLogFile.Rows.Remove(selectedRow)
      Next selectedRow
   End Sub

    Private Sub LogFileGeneratorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LogFileGeneratorToolStripMenuItem.Click
        LogFileGenerator.Show()
    End Sub

    Private Sub dgvLogFile_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvLogFile.CellContentClick

    End Sub

    Private Sub ExcelToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExcelToolStripMenuItem.Click
        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value
        Dim i As Integer
        Dim j As Integer

        xlApp = New Excel.ApplicationClass
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("Φύλλο1")


        Dim iC As Short
        xlWorkSheet.Cells.Select()
        xlWorkSheet.Cells.Delete()
        For iC = 0 To dgvLogFile.Columns.Count - 1
            xlWorkSheet.Cells(1, iC + 1).Value = dgvLogFile.Columns(iC).HeaderText

        Next


        For i = 0 To dgvLogFile.RowCount - 1
            For j = 0 To dgvLogFile.Columns.Count - 1
                xlWorkSheet.Cells(i + 2, j + 1) = dgvLogFile.Rows(i).Cells(j).Value
            Next
        Next

        xlWorkSheet.SaveAs("C:\Users\lalala\Desktop\vbexcel.xls")
        xlWorkBook.Close()
        xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

        MsgBox("You can find the file C:\vbexcel.xls")

    End Sub


    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub
End Class
